#include <QDir>
#include <QPainter>
#include <QMouseEvent>

#include "minionselectiondialog.h"
#include "ui_minionselectiondialog.h"
#include "mainwindow.h"

MinionSelectionDialog::MinionSelectionDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MinionSelectionDialog)
{
    ui->setupUi(this);

    CountdownTimer.setInterval(1000);
    connect(&CountdownTimer, SIGNAL(timeout()), this, SLOT(CountdownTick()));
}

MinionSelectionDialog::~MinionSelectionDialog()
{
    delete ui;
}

void MinionSelectionDialog::LoadAvailableMinions()
{
    AvailableMinions.clear();
    QStringList Folders = QDir("Minions").entryList(QDir::AllDirs|QDir::NoDotAndDotDot);
    foreach(QString Folder, Folders)
    {
        AvailableMinions.append(QPair<QString, QPixmap>(Folder, QPixmap("Minions/" + Folder + "/selection.png")));
    }
}

void MinionSelectionDialog::Initialize(GameSettings &Settings)
{
    LoadAvailableMinions();

    setVisible(true);
    ui->labelCountdown->setText("30");
    PreGameCountdown = 5;
    CurrentSettings = Settings;
    for(int team = 0; team < 2; ++team)
    for(int slot = 0; slot < CurrentSettings.Teams[team].count(); ++slot)
        CurrentSettings.Teams[team][slot].MinionName = AvailableMinions[0].first;

    ui->sliderMinionSelection->setValue(0);
    if(AvailableMinions.count() > 8)
    {
        ui->sliderMinionSelection->setVisible(true);
        ui->sliderMinionSelection->setMaximum(AvailableMinions.count() - 8);
    }
    else
        ui->sliderMinionSelection->setHidden(true);
    repaint();

    CountdownTimer.start();
}

void MinionSelectionDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    for(int i=0; i < 8 && i < AvailableMinions.count(); i++)
    {
        painter.drawPixmap(ui->MinionSelection->x() + 10 + 70 * i,
                           ui->MinionSelection->y() + 10,
                           60, 60,
                           AvailableMinions[i + ui->sliderMinionSelection->sliderPosition()].second);
        painter.drawText(ui->MinionSelection->x() + 10 + 70 * i,
                         ui->MinionSelection->y() + 75,
                         60, 15, Qt::AlignHCenter,
                         AvailableMinions[i + ui->sliderMinionSelection->sliderPosition()].first);

        if(AvailableMinions[i + ui->sliderMinionSelection->sliderPosition()].first ==
           CurrentSettings.Teams[CurrentSettings.MyTeam][CurrentSettings.MyPosition].MinionName)
        {
            painter.setPen(QPen(QBrush(Qt::red), 2));
            painter.drawRect(ui->MinionSelection->x() + 10 + 70 * i,
                             ui->MinionSelection->y() + 10, 60, 60);
            painter.setPen(Qt::black);
        }
    }
}

void MinionSelectionDialog::mousePressEvent(QMouseEvent *e)
{
    int x = e->localPos().x() - ui->MinionSelection->x() - 10;
    int y = e->localPos().y() - ui->MinionSelection->y() - 10;

    if(x >= 0 &&  x < ui->MinionSelection->width() - 10 &&
       y >= 0 && y <= ui->MinionSelection->height() - 60)
    {
        if(x % 70 <= 60)
        {
            CurrentSettings.Teams[CurrentSettings.MyTeam][CurrentSettings.MyPosition].MinionName =
                    AvailableMinions[x/70 + ui->sliderMinionSelection->sliderPosition()].first;
            repaint();
        }
        // else clicked between minion pictures
    }
}

void MinionSelectionDialog::CountdownTick()
{
    PreGameCountdown--;
    ui->labelCountdown->setText(QString("%1").arg(PreGameCountdown));

    if(PreGameCountdown == 0)
    {
        CountdownTimer.stop();
        setHidden(true);
        ((MainWindow*)parent())->StartGame(CurrentSettings);
    }
}

void MinionSelectionDialog::on_sliderMinionSelection_valueChanged(int)
{
    repaint();
}
